package com.tjjingpan.broadcastbestpractice

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_login.accountEdit
import kotlinx.android.synthetic.main.activity_login.login
import kotlinx.android.synthetic.main.activity_login.passwrodEdit
import kotlinx.android.synthetic.main.login_activity.*

class AnotherLoginActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)
        val prefs = getPreferences(Context.MODE_PRIVATE)
        val isRemember = prefs.getBoolean("remember_password",false)
        if(isRemember)
        {
            val account = prefs.getString("account","")
            val password = prefs.getString("password","")
            accountEdit.setText(account)
            passwrodEdit.setText(password)
            rememberPass.isChecked= true
        }
        login.setOnClickListener {
            val account = accountEdit.text.toString()
            val password = passwrodEdit.text.toString()
            //如果账号是admin且密码是123456,就认为登录成功
            if(account == "admin" && password=="123456"){
                val editor = prefs.edit()
                if(rememberPass.isChecked){ //检查复选框是否被选中
                    editor.putBoolean("remember_password",true)
                    editor.putString("account",account)
                    editor.putString("password",password)
                }else
                {
                    editor.clear()
                }
                editor.apply()
                val intent = Intent(this,MainActivity::class.java)
                startActivity(intent)
                finish()
            }else{
                Toast.makeText(this,"account or password is invalid", Toast.LENGTH_SHORT).show()
            }
        }
    }
}